package org.jeecg.modules.monitoring.j_monitoring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.utils.EquipmentUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlRegulator;
import org.jeecg.modules.monitoring.j_monitoring.entity.format.MountCabinetParam;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlRegulatorService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 控制器
* @Author: xiaoyq
* @Date:   2023-05-25
* @Version: V1.0
*/
@Api(tags="控制器")
@RestController
@RequestMapping("/j_monitoring/mGlRegulator")
@Slf4j
public class MGlDriveRegulatorController extends JeecgController<MGlRegulator, IMGlRegulatorService> {

   /**
    * 分页列表查询
    *
    * @param mGlRegulator
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "控制器-分页列表查询")
   @RequiresPermissions("控制器:查询")
   @ApiOperation(value="控制器-分页列表查询", notes="控制器-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<MGlRegulator>> queryPageList(MGlRegulator mGlRegulator,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<MGlRegulator> queryWrapper = QueryGenerator.initQueryWrapper(mGlRegulator, req.getParameterMap());
       Page<MGlRegulator> page = new Page<MGlRegulator>(pageNo, pageSize);

       IPage<MGlRegulator> pageList = service.page(page, queryWrapper);
       List<MGlRegulator> networkResourceList  = pageList.getRecords();
       for (MGlRegulator computingResource : networkResourceList){
           computingResource.setMonitorStatus(EquipmentUtils.getMonitorStatus(computingResource.getWhetherToMonitor()));
       }
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param mGlRegulator
    * @return
    */
   @AutoLog(value = "控制器-添加")
   @ApiOperation(value="控制器-添加", notes="控制器-添加")
   @RequiresPermissions("控制器:添加")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody MGlRegulator mGlRegulator) {
       service.save(mGlRegulator);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param mGlRegulator
    * @return
    */
   @AutoLog(value = "控制器-编辑")
   @ApiOperation(value="控制器-编辑", notes="控制器-编辑")
   @RequiresPermissions("控制器:编辑")
   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
   public Result<String> edit(@RequestBody MGlRegulator mGlRegulator) {
       service.updateById(mGlRegulator);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "控制器-通过id删除")
   @ApiOperation(value="控制器-通过id删除", notes="控制器-通过id删除")
   @RequiresPermissions("控制器:删除")
   @DeleteMapping(value = "/delete")
   public Result<String> delete(@RequestParam(name="id",required=true) String id) {
       service.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "控制器-批量删除")
   @ApiOperation(value="控制器-批量删除", notes="控制器-批量删除")
   @RequiresPermissions("控制器:批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.service.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   //@AutoLog(value = "控制器-通过id查询")
   @ApiOperation(value="控制器-通过id查询", notes="控制器-通过id查询")
   @GetMapping(value = "/queryById")
   @RequiresPermissions("控制器:查询")
   public Result<MGlRegulator> queryById(@RequestParam(name="id",required=true) String id) {
       MGlRegulator mGlRegulator = service.getById(id);
       if(mGlRegulator==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(mGlRegulator);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param mGlRegulator
   */
   @RequiresPermissions("控制器:导出")
   @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    @ApiOperation(value="控制器-导出", notes="控制器-导出")
   public ModelAndView exportXls(HttpServletRequest request, MGlRegulator mGlRegulator) {
       return super.exportXls(request, mGlRegulator, MGlRegulator.class, "控制器");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequiresPermissions("控制器:导入")
   @ApiOperation(value="控制器-导入", notes="控制器-导入")
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, MGlRegulator.class);
   }

    @RequestMapping(value = "/mountCabinet", method = RequestMethod.POST)
    public Result<?> mountCabinet(@RequestBody MountCabinetParam mGlRegulator) {
        return service.mountCabinet(mGlRegulator);
    }


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test() {
        service.test();
    }

    @Override
    protected  void beforeImportExcel(List<MGlRegulator> list){
        list.forEach(item->{
            if(org.apache.commons.lang.StringUtils.isNotEmpty(item.getCabinet())){
                item.setCabinet(item.getCabinet().trim());
            }
            if(org.apache.commons.lang.StringUtils.isNotEmpty(item.getMachineRoom())){
                item.setMachineRoom(item.getMachineRoom().trim());
            }
            if(org.apache.commons.lang.StringUtils.isNotEmpty(item.getPersonLiable())){
                item.setPersonLiable(item.getPersonLiable().trim());
            }
        });
    }

    @Override
    protected void afterImportExcel(List<MGlRegulator> list){
        UpdateWrapper<MGlRegulator> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().isNull(MGlRegulator::getCabinetId);
        updateWrapper.setSql("Cabinet_id=(select id from m_cabinet where Cabinet=name LIMIT 1)");
        this.service.update(updateWrapper);

        updateWrapper.clear();
        updateWrapper.lambda().isNull(MGlRegulator::getMachineRoomId);
        updateWrapper.setSql("Machine_Room_Id=(select id from m_machine_room where machine_room=name LIMIT 1)");
        this.service.update(updateWrapper);

        updateWrapper.clear();
        updateWrapper.lambda().isNull(MGlRegulator::getPersonLiableId);
        updateWrapper.setSql("person_liable_id=(select id from sys_user where person_liable=realname LIMIT 1)");
        this.service.update(updateWrapper);
    }
}
